/**
 * 
 * @author 	demonshalo
 * @host	code.google.com
 * @created	02/21/2012
 * 
 * RSSParser is a simple JAVA RSS reader.
 * PLEASE READ ReadMe.txt BEFORE WORKING WITH THIS PACKAGE! 
 * 
 * ANDROID: 
 * 		The code will work as is for API level 8 and above.
 * 		Please check ReadMe.txt for info about API level 7 and lower.
 *
 */


package com.lorenum.rss.reader;

import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class RSSParser {
	
	public static Feed parse(String URL) {
		//Create feed instance
		Feed feed = new Feed();
		
		try{
			
			//Create URL
			URL url = new URL(URL);
			
			//Create XML document from URL
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(url.openStream());
			
			//Where the document root will be saved (i.e. <RSS>)
			NodeList root = null;
			
			//Grab all the first nodes (ex. <rss>, <?xml ?>, etc)
			NodeList list = (NodeList) doc.getChildNodes();
			
			//Iterate through all the upper nodes
			for(int i = 0; i < list.getLength(); i++){
				
				//Get the <rss> node and set it as document root
				if(list.item(i).getNodeName().equals("rss"))
					root = list.item(i).getChildNodes();
			}
			
			if(root != null){
				NodeList channel = null; 
					
				//iterate over <rss> elements
				for(int i = 0; i < root.getLength(); i++){
					
					//Grab the <channel> element
					if(root.item(i).getNodeName().equals("channel"))
						channel = (NodeList) root.item(i).getChildNodes();

				}
				
				for(int j = 0; j < channel.getLength(); j++){
					Node node = channel.item(j);

					//Grab channel title
					if(node.getNodeName().equals("title"))
						feed.setTitle(node.getTextContent());
					
					//Grab channel link
					else if(node.getNodeName().equals("link"))
						feed.setLink(node.getTextContent());						
					
					//Grab channel description
					else if(node.getNodeName().equals("description"))
						feed.setDescription(node.getTextContent());
					
					//Grab channel pubDate
					else if(node.getNodeName().equals("pubDate"))
						feed.setPubDate(node.getTextContent());

					//Grab channel item(s)
					else if(node.getNodeName().equals("item")){
						
						//Create feed entry
						FeedEntry entry = new FeedEntry();
						
						//Loop through the <item> inner nodes (ex. <title>, <description, etc)
						for(int p = 0; p < node.getChildNodes().getLength(); p++){
							
							//Grab the inner node
							Node itemNode = node.getChildNodes().item(p);
							
							//Grav the inner node title
							if(itemNode.getNodeName().equals("title"))
								entry.setTitle(itemNode.getTextContent());
							
							else if(itemNode.getNodeName().equals("description"))
								entry.setDescription(itemNode.getTextContent());
							
							else if(itemNode.getNodeName().equals("link"))
								entry.setLink(itemNode.getTextContent());
							
							else if(itemNode.getNodeName().equals("author"))
								entry.setAuthor(itemNode.getTextContent());		
							
							else if(itemNode.getNodeName().equals("pubDate"))
									entry.setPubDate(itemNode.getTextContent());
							
							else if(itemNode.getNodeName().equals("guid"))
								entry.setGuid(itemNode.getTextContent());
						}
						
						feed.addEntry(entry);
					}
				}
			}	
		}
		catch(Exception e){}
		
		return feed;
	}
}
